package com.wego.controller.manager;

import com.wego.common.bean.PageBean;
import com.wego.common.bean.ResultBean;
import com.wego.common.enumeration.ResultEnum;
import com.wego.common.ex.GlobalException;
import com.wego.common.utils.QueryUtil;
import com.wego.common.utils.ResultBeanUtil;
import com.wego.entity.domain.Province;
import com.wego.entity.query.ProvinceQuery;
import com.wego.service.ProvinceService;
import java.time.LocalDateTime;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * 省份控制器
 * @author: hc
 * @date: 2023/7/2
 */
@Slf4j
@Controller
@RequestMapping("/manager/province")
public class ProvinceController {
    @Autowired
    private ProvinceService provinceService;

    /**
     * 省份列表
     * @param provinceQuery
     * @param model
     * @param resultBean
     * @return
     */
    @GetMapping("/list")
    public String list(ProvinceQuery provinceQuery, Model model, @ModelAttribute("resultBean") ResultBean resultBean) {
        //构造动态查询条件
        String name = provinceQuery.getName();
        if (name == null) {
            String keywords = provinceQuery.getKeywords();
            provinceQuery.setName(keywords);
        }

        PageBean<Province> pageBean = provinceService.selectPage(provinceQuery);

        //分页时携带上查询条件
        String params = QueryUtil.genQueryCondition(provinceQuery);
        pageBean.setUrl("manager/province/list?" + params);
        model.addAttribute("pageBean", pageBean);

        //查询条件数据回显时使用
        model.addAttribute("provinceQuery", provinceQuery);

        //增删改操作完返回到列表页面的提示信息
        if (resultBean != null) {
            model.addAttribute("resultBean", resultBean);
        }

        return "manager/province";
    }

    /**
     * 省份详情
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/details")
    public String details(Long id, Model model) {
        Province province = provinceService.selectByPrimaryKey(id);

        model.addAttribute("province", province);

        return "manager/province_details";
    }

    /**
     * 打开添加修改页面
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/openAddUpdate")
    public String openAddUpdate(Long id, Model model) {
        Province province = provinceService.selectByPrimaryKey(id);
        model.addAttribute("province", province);

        if (id != null) {
            model.addAttribute("title", "修改省份");
        } else {
            model.addAttribute("title", "添加省份");
        }

        return "manager/province_add_update";
    }

    /**
     * 具体执行添加修改操作
     * @param province
     * @return
     */
    @PostMapping("/doAddUpdate")
    public String doAddUpdate(Province province, RedirectAttributes redirectAttributes) {
        province.setUpdateTime(LocalDateTime.now());

        ResultBean resultBean;
        Long id = province.getId();
        if (id != null) {
            //修改
            boolean res = provinceService.updateByPrimaryKeySelective(province);
            resultBean = ResultBeanUtil.build(res, ResultEnum.UPDATE_SUCCESS, ResultEnum.UPDATE_EXCEPTION);
        } else {
            //添加
            province.setCreateTime(LocalDateTime.now());
            boolean res = provinceService.insertSelective(province);
            resultBean = ResultBeanUtil.build(res, ResultEnum.INSERT_SUCCESS, ResultEnum.INSERT_EXCEPTION);
        }

        //返回列表页面时，对话框中显示的提示信息
        redirectAttributes.addFlashAttribute("resultBean", resultBean);

        //重定向
        return "redirect:list";
    }

    /**
     * 修改状态
     * @param province
     * @return
     */
    @GetMapping("/changeState")
    public String changeState(Province province, RedirectAttributes redirectAttributes) {
        Integer state = province.getState();
        Long id = province.getId();
        if (state == null && id == null) {
            //执行修改状态时提交的参数不正确
            throw new GlobalException(ResultEnum.PARAM_ERROR, "list");
        }

        //修改状态的值
        province.setState(state == 1 ? 0 : 1);
        province.setUpdateTime(LocalDateTime.now());

        boolean res = provinceService.updateByPrimaryKeySelective(province);

        //返回列表页面时，对话框中显示的提示信息
        ResultBean resultBean = ResultBeanUtil.build(res, ResultEnum.UPDATE_SUCCESS, ResultEnum.UPDATE_EXCEPTION);
        //返回列表页面时，对话框中显示的提示信息
        redirectAttributes.addFlashAttribute("resultBean", resultBean);

        return "redirect:list";
    }

    /**
     * 根据主键删除
     * @param id
     * @return
     */
    @GetMapping("/deleteById")
    public String deleteById(Long id, RedirectAttributes redirectAttributes) {
        boolean res = provinceService.deleteByPrimaryKey(id);

        //返回列表页面时，对话框中显示的提示信息
        ResultBean resultBean = ResultBeanUtil.build(res, ResultEnum.DELETE_SUCCESS, ResultEnum.DELETE_EXCEPTION);
        //返回列表页面时，对话框中显示的提示信息
        redirectAttributes.addFlashAttribute("resultBean", resultBean);

        return "redirect:list";
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @GetMapping("/batchDelete")
    public String batchDelete(String ids, RedirectAttributes redirectAttributes) {
        boolean res = provinceService.batchDelete(ids);

        ResultBean resultBean = ResultBeanUtil.build(res, ResultEnum.DELETE_SUCCESS, ResultEnum.DELETE_EXCEPTION);
        //返回列表页面时，对话框中显示的提示信息
        redirectAttributes.addFlashAttribute("resultBean", resultBean);

        return "redirect:list";
    }
}
